<?php
/***
 * $sql="SELECT op_value FROM ".$this->ecs->table('options')." WHERE op_key='appid'";
 * $is_appid=$this->db->getOne($sql);
 */

class goodsModel{
	private $db;
	private $ecs;
	public function __construct(){
		$this->db = $GLOBALS['db'];
		$this->ecs = $GLOBALS['ecs'];
	}
	
	public function get_goods_list($cfg){
		$page = $cfg['page'];
		if($cfg['cat_id'] == "ALL"){
			$where = " 1=1 ";
		}else{
			$where = " cat_id = ".$cfg['cat_id'];
		}
		if($cfg['key']!=""){
			$where.=" AND goods_name like '%".$cfg['key']."%' ";
		}
		$order = " ORDER BY ".$cfg['order']." desc";
		$pageSize = constant("PAGE_SIZE");
		$pageNum = ($page-1)*$pageSize;
		$sql="SELECT goods_id,goods_name,goods_brief,market_price,shop_price,goods_thumb,goods_img,original_img FROM ".$this->ecs->table("goods")." WHERE $where $order LIMIT ".$pageNum.",".$pageSize;
		$result['goods_list']  = $this->db->getAll($sql);
		return $result;
	}
	
	public function get_goods_info($id){
		if ($id==0) {
			$goods_id = $this->db->getOne ( "select goods_id from " . $this->ecs->table ( 'goods' ) . " where is_on_sale = 1 and is_delete = 0 order by sort_order" );
		} else {
			$goods_id =$id;
		}
		$user_id = $_GET ['user_id'] > 0 ? $_GET ['user_id'] : 0;
		
		$goods_info = get_goods_info ( $goods_id );
		
		$sql = " select count(*) from   " . $this->ecs->table ( 'collect_goods' ) . ' AS g ' . 

		"WHERE g.goods_id = '$goods_id' AND g.user_id = '$user_id' ";
		
		$goods_info ['collect'] = $this->db->GetOne ( $sql );
		
		// $linked_goods = get_linked_goods($goods_id);
		
		//$goods_info ['month_nums'] = encode_output ( ec_buysum ( $goods_id ) );
		
		$goods_info ['goods_name'] = htmlspecialchars ( $goods_info ['goods_name'] );
		
		$goods_info ['goods_desc'] = htmlspecialchars ( $goods_info ['goods_desc'] );
		
		// $goods_info['goods_desc'] = encode_output($goods_info['goods_desc']);
		
		$goods_info ['goods_desc'] = preg_replace ( "/<a[^>]*>(.*)<\/a>/isU", '${1}', $goods_info ['goods_desc'] );
		
		$goods_info ['goods_desc'] = htmlspecialchars_decode ( str_replace ( '/images', 'http://' . $_SERVER ['HTTP_HOST'] . "/images", $goods_info ['goods_desc'] ) );
		
		// echo $goods_info['goods_desc'] ;
		
		// exit;
		$goods_info ['promote_price'] = htmlspecialchars ( $goods_info ['promote_price'] );
		
		$goods_info ['market_price'] = htmlspecialchars ( $goods_info ['market_price'] );
		
		$goods_info ['shop_price'] = htmlspecialchars ( $goods_info ['shop_price'] );
		
		$goods_info ['shop_price_formated'] = htmlspecialchars ( $goods_info ['shop_price_formated'] );
		
		$goods_info ['goods_number'] = htmlspecialchars ( $goods_info ['goods_number'] );
		
// 		$comment = get_goods_comment ( $goods_id );
		
// 		$goods_info ['comment'] = $comment;
		
		$goods_info ['comment_nums'] = count ( $comment );
		
		$properties = get_goods_properties ( $goods_id ); // 获得商品的规格和属性
		
		$goods_info ['properties'] = $properties;
		
		$goods_info ['gallery'] = get_goods_gallery ( $goods_id );
		return $goods_info;
	}
	
	function get_goods_comment($goods_id,$num=100,$start=0)
	
	{
	
		/* 取得评论列表 */
	
	
	
		$sql = 'SELECT p1.* , p2.user_name  FROM ' . $this->ecs->table('comment') .' as p1 '.
				'left join '. $this->ecs->table('users') .' as p2 on p1.user_id = p2.user_id '.
				// 	'left join '. $GLOBALS['ecs']->table('order_goods') .' as p3 on p1.goods_id = p3.goods_id  '.
		// 	'left join '. $GLOBALS['ecs']->table('goods') .' as p4 on p1.goods_id = p4.goods_id  '.
		" WHERE p1.goods_id = '$goods_id'  AND p1.parent_id = 0  and p1.status =1 ".
	
		' ORDER BY p1.add_time DESC';
	
		$res = $this->db->SelectLimit($sql, $num, $start);
	
		// 	$res = $GLOBALS['db']->query($sql);
	
	
	
		$arr = array();
	
		$ids = '';
	
		while ($row = $this->db->fetchRow($res))
	
		{
	
			$ids .= $ids ? ",$row[comment_id]" : $row['comment_id'];
	
			$temp = array();
	
			$temp['comment_id']       = $row['comment_id'];
	
			$temp['user_name'] = $row['user_name'];
	
			// 		$temp['goods_name'] = $row['goods_name'];
	
			// 		$temp['goods_price'] = $row['goods_price'];
	
			// 		$temp['goods_number'] = $row['goods_number'];
	
			// 		$temp['original_img'] = $row['original_img'];
	
			$temp['content']  = str_replace('\r\n', '<br />', htmlspecialchars($row['content']));
	
			$temp['content']  = nl2br(str_replace('\n', '<br />', $temp['content']));
	
			$temp['comment_rank']     = $row['comment_rank'];
	
			$temp['add_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['add_time']);
	
			$arr[] = $temp;
	
		}
	
		return $arr;
	
	}
}